/**
 * 
 */
package datatype.DBtypes;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * @author Yuval
 * 
 * 
 *         this is a class that the db should return after choosing a program in
 *         search or, this class should be created to insert new program to the
 *         db
 * 
 */
public class ProgramfromDB {

	private int id = 0;
	private String name = null;
	// start and finish dates
	private int sYear = 0, fYear = 0, sMonth = 0, fMonth = 0, sDay = 0,
			fDay = 0;
	private int numOfEpisodes = 0;
	private int numOfSeasons = 0;
	private String origanCountry = null;
	// private genre..
	// private creator..
	private List<Actor> actors = new ArrayList<Actor>();
	private List<Creator> creators = new ArrayList<Creator>();

	// we will send this list to reduce the size and overhead
	private List<Actor> newActors = new ArrayList<Actor>();
	private List<Actor> editedActors = new ArrayList<Actor>();
	private List<Creator> newcreators = new ArrayList<Creator>();

	public ProgramfromDB(int id, String name, int sYear, int fYear,
			int sMounth, int fMounth, int sDay, int fDay, int numOfEpisodes,
			int numOfSeasons, String origanCountry) {
		super();
		this.id = id;
		this.name = name;
		this.sYear = sYear;
		this.fYear = fYear;
		this.sMonth = sMounth;
		this.fMonth = fMounth;
		this.sDay = sDay;
		this.fDay = fDay;
		this.numOfEpisodes = numOfEpisodes;
		this.numOfSeasons = numOfSeasons;
		this.origanCountry = origanCountry;
	}

	public ProgramfromDB() {
	}

	public synchronized List<Actor> getActors() {
		return actors;
	}

	public synchronized List<Actor> getNewActors() {
		return newActors;
	}

	public synchronized List<Creator> getCreators() {
		return creators;
	}

	public synchronized List<Creator> getNewcreators() {
		return newcreators;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getNumOfEpisodes() {
		return numOfEpisodes;
	}

	public void setNumOfEpisodes(int numOfEpisodes) {
		this.numOfEpisodes = numOfEpisodes;
	}

	public int getNumOfSeasons() {
		return numOfSeasons;
	}

	public void setNumOfSeasons(int numOfSeasons) {
		this.numOfSeasons = numOfSeasons;
	}

	public String getOriganCountry() {
		return origanCountry;
	}

	public void setOriganCountry(String origanCountry) {
		this.origanCountry = origanCountry;
	}

	public synchronized void addActor(int id, String name, String character,
			int from, int to, boolean isCreation) {
		if (isCreation)
			actors.add(new Actor(id, name, character, from, to));
		else
			newActors.add(new Actor(id, name, character, from, to));
	}
	public synchronized void editActor(int id, String name, String character) {
		editedActors.add(new Actor(id, name, character,0,0));
}

	public synchronized void addCreator(int id, String name, boolean isCreation) {
		if (isCreation)
			creators.add(new Creator(id, name));
		else
			newcreators.add(new Creator(id, name));
	}
	
	public int getActorID(int actorinList){
		if(actorinList<actors.size())
		return actors.get(actorinList).getId();
		else
			return newActors.get(actorinList-actors.size()).getId();
	}
	public String getActorName(int actorinList){
		if(actorinList<actors.size())
		return actors.get(actorinList).getName();
		else
			return newActors.get(actorinList-actors.size()).getName();
	}

	public void prepareActorTable(Table table) {

		for (Actor a : actors) {
			TableItem ti = new TableItem(table, SWT.NONE);
			ti.setText(0, a.getName());
			ti.setText(1, a.getCharacter());
		}
	}
	
	public String getDate(boolean isStart){
		
		String date;
		if(isStart)
			date = ""+sYear+"-"+ (sMonth<10? "0"+sMonth: ""+sMonth) +"-"+ (sDay<10? "0"+sDay: ""+sDay) ;
		else
			date = ""+fYear+"-"+ (fMonth<10? "0"+fMonth: ""+fMonth) +"-"+ (fDay<10? "0"+fDay: ""+fDay) ;
		return date;
	}

	public void prepareCreatorTable(Table table) {

		for (Creator c : creators) {
			TableItem ti = new TableItem(table, SWT.NONE);
			ti.setText(0, c.getName());
		}
	}

	public void setStartDate(int y, int m, int d) {
		sYear = y;
		sMonth = m;
		sDay = d;
	}

	public void setFinishDate(int y, int m, int d) {
		fYear = y;
		fMonth = m;
		fDay = d;
	}
	
	/* 
	 * creates the query for updating the DB
	 */
	
	public List<String> getQuery(int programID, boolean isActor){
		
		List<String> Query = new ArrayList<String>();
		int ID = id==0? programID: id;
		Random r = new Random(); 
		if(isActor)
		for(Actor a: newActors){
			int rand = r.nextInt();
			Query.add("Insert Into TV_Play(`Character`, Program_Id, Actor_Id, mid) Values('"+a.getCharacter()+"', "+ID+" , "+a.getId()+",'"+System.currentTimeMillis()+""+rand%999999999+"')");
		}else
			for(Creator c: newcreators){
				Query.add("Insert Into Created(Program_Id, Program_Creator_Id) Values("+ID+" , "+c.id+")");
			}
		for(Actor a: editedActors){
		//	System.out.println("Update TV_Play SET `Character` = '"+a.getCharacter()+"' where Actor_Id = '"+a.getId()+"'");
			Query.add("Update TV_Play SET `Character` = '"+a.getCharacter()+"' where Actor_Id = '"+a.getId()+"'");
		}
				
		return Query;
		
	}
	

	public int getsYear() {
		return sYear;
	}

	public int getfYear() {
		return fYear;
	}

	public int getsMonth() {
		return sMonth;
	}

	public int getfMonth() {
		return fMonth;
	}

	public int getsDay() {
		return sDay;
	}

	public int getfDay() {
		return fDay;
	}

	private static class Actor {

		private int id = 0;
		private String name = null;
		private String character = null;
		private int from = 0;
		private int to = 0;

		public Actor(int id, String name, String character, int from, int to) {
			super();
			this.id = id;
			this.name = name;
			this.character = character;
			this.from = from;
			this.to = to;
		}

		public int getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public String getCharacter() {
			return character;
		}

	}

	private static class Creator {

		private int id = 0;
		private String name = null;

		public Creator(int id, String name) {
			super();
			this.id = id;
			this.name = name;
		}

		public String getName() {
			return name;
		}
	}

	// TODO
	public int getYear() {
		
		return sYear;
	}

}
